package com.ouyang.controller;

import com.ouyang.Service.CdbService;
import com.ouyang.form.AddOrderForm;
import com.ouyang.poji.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/cdb")
public class CdbController {

    @Autowired
    private CdbService cdbService;

    @RequestMapping("/cdblist")
    public String getcdblist ( Model model ) {
        List<Orders> list = cdbService.getcdblist();
        model.addAttribute("list", list);
        return "cdblist";
    }

    @RequestMapping("/search")
    public String search ( String key, String value, Model model ) {
        List<Orders> list = cdbService.search(key, value);
        model.addAttribute("list", list);
        return "cdblist";
    }

    @RequestMapping("/add")
    public String add ( AddOrderForm orderForm ) {
        try {
            cdbService.add(orderForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/cdb/cdblist";
    }

    @RequestMapping("/update")
    public String update ( Orders orders ) {
        try {
            cdbService.update(orders);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/cdb/cdblist";
    }

    @RequestMapping("/delete")
    public String delete ( String id ) {
        try {
            cdbService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/cdb/cdblist";
    }
}
